/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vsnds;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rexcze
 */
public class UbuntuConsole implements Vykonavatel {
    private ArrayList <Prikaz> prikazy = new ArrayList<Prikaz>();

    public VystupPrikazu proved(Prikaz prikaz) {
        String s;
        VystupPrikazu v = new VystupPrikazu();
        try {
            Process p = Runtime.getRuntime().exec(prikaz.prikaz+" "+prikaz.parametry);
             BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            while ((s = stdInput.readLine()) != null) {
                v.stdOut += s+"\n";
            }

            // read any errors from the attempted command
            while ((s = stdError.readLine()) != null) {
                v.errOut += s+"\n";
            }
            //doplnit navratovou hodnotu

        } catch (IOException ex) {
            Logger.getLogger(UbuntuConsole.class.getName()).log(Level.SEVERE, null, ex);
        }
        return v;
    }

    public UbuntuConsole() {
        this.prikazy.add(new Prikaz(1, "pacman", "-Su"));
        this.prikazy.add(new Prikaz(2, "ping", "google.com -c 1"));
        this.prikazy.add(new Prikaz(3, "df", "-h"));
    }

    public Prikaz vratPrikaz(int cislo) {
        System.out.println("  zjistuji prikaz..");
        //zjistime, jestli prikaz umime
        //najdi podle cisla ten co hledam a vrat ho
        for (Prikaz prikaz : this.prikazy) {
            System.out.println("  prikaz match na "+cislo+": "+prikaz.cislo);
            if(prikaz.cislo == cislo){
                return prikaz;
            }
        }
        // vyhod vyjimku, ze ho nezna, treba objekt vyjimka!! dodelat
        return null;
    }


}
